<template>
    <div>
        <TableSearch :query="query" :options="searchOpt" :search="handleSearch" />
        <div class="container">

            <TableCustom :columns="columns" :tableData="tableData" :total="page.total" :viewFunc="handleView"
                :delFunc="handleDelete" :changePage="changePage" :editFunc="handleEdit">
                <template #type="{ rows }">

                    <span>{{ rows.type == 1 ? '会员' : (rows.type == 2 ? '星星' : '能量租赁') }}</span>
                </template>
                <template #status="{ rows }">
                    <el-tag type="success" v-if="rows.status">上架</el-tag>
                    <el-tag type="danger" v-else>下架</el-tag>
                </template>
                <template #operator1="{rows}">
                    <el-button type="warning" size="small" @click="handleView(rows)">详情</el-button>
                    <el-button type="primary" size="small" @click="changeSalePrice(rows)"
                        v-if="agent==='1'">修改销售价格</el-button>
                </template>
            </TableCustom>
        </div>
        <el-dialog :title="isEdit ? '编辑' : '新增'" v-model="visible" width="700px" destroy-on-close
            :close-on-click-modal="false" @close="closeDialog">
            <TableEdit :form-data="rowData" :options="options" :edit="isEdit" :update="updateData" />
        </el-dialog>
        <el-dialog title="查看详情" v-model="visible1" width="700px" destroy-on-close>
            <TableDetail :data="viewData">
                <template #status="{ rows }">
                    <el-tag type="success" v-if="rows.status">上架</el-tag>
                    <el-tag type="danger" v-else>下架</el-tag>
                </template>
                <template #type="{ rows }">
                    <span>{{ rows.type == 1 ? '会员' : (rows.type == 2 ? '星星' : '能量租赁') }}</span>
                </template>
            </TableDetail>
        </el-dialog>
        <el-dialog title="销售价格" v-model="visiblePrice" width="300px" destroy-on-close :close-on-click-modal="false"
            @close="closePriceDialog">
            <div class="salePrice">
                <span>当前销售价 ：</span> <el-input v-model="sale.price" disabled />
            </div>
            <div class="salePrice">
                <span>修改销售价 ：</span> <el-input v-model="sale.sale_price" placeholder="修改价格" />
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitPrice">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
    import { ref, reactive } from 'vue';
    import { ElMessage } from 'element-plus';
    import { Role } from '@/types/role';
    import { agentStarGoodsList, agentModifyStarGoodsPrice, agentStarGoodsDetail } from '@/api'; // Updated function name
    import TableCustom from '@/components/table-custom.vue';
    import TableDetail from '@/components/table-detail.vue';
    import { CirclePlusFilled } from '@element-plus/icons-vue';
    import { FormOption, FormOptionList } from '@/types/form-option';

    // 查询相关
    const query = reactive({
        title: '',
        status: '',
        type: 3
    })
    const searchOpt = ref < FormOptionList[] > ([
        { type: 'input', label: '商品名称', prop: 'title' },
        {
            type: 'select', label: '状态', prop: 'status', opts: [
                { label: '上架', value: 1 },
                { label: '下架', value: 0 }
            ]
        },
        // {
        //     type: 'select', label: '商品分类', prop: 'type', opts: [
        //         { label: '会员', value: 1 },
        //         { label: '星星', value: 2 },
        //         { label: '能量租赁', value: 3 }
        //     ]
        // }
    ])
    const handleSearch = () => {
        page.title = query.title
        page.status = query.status
        page.type = query.type; // Add this line to include type in the search
        changePage(1);
    };
    let sale = ref({
        price: '',
        sale_price: '',
        goods_id: ''
    })
    // 表格相关
    let columns = ref([
        { prop: 'id', label: '商品ID', width: 140 },
        { prop: 'type', label: '商品分类' },
        { prop: 'title', label: '商品名称' },
        { prop: 'status', label: '状态', },
        { prop: 'month', label: '月数/笔数' },
        { prop: 'cost_price', label: '成本价' },
        { prop: 'sale_price', label: '销售价' },
        { prop: 'profit', label: '利润' },
        { prop: 'operator1', label: '操作项', width: 200 },
    ])

    let agent = localStorage.getItem('agent');
    const page = reactive({
        page: 1,
        limit: 10,
        total: 0,
        status: undefined,
        title: undefined,
        type: 3 // Add this line to include type in the page object
    })
    const tableData = ref < Role[] > ([]);
    const getData = async () => {

        const res = await agentStarGoodsList(page)
        tableData.value = res.data.data;
        page.total = res.data.total
    };
    getData();
    const changePage = (val: number) => {
        page.page = val;
        getData();
    };

    // 新增/编辑弹窗相关
    const options = ref < FormOption > ({
        labelWidth: '100px',
        span: 24,
        list: [
            { type: 'input', label: '角色名称', prop: 'name', required: true },
            { type: 'input', label: '角色标识', prop: 'key', required: true },
            { type: 'switch', label: '状态', prop: 'status', required: false, activeText: '启用', inactiveText: '禁用' },
        ]
    })
    const visible = ref(false);
    const visiblePrice = ref(false);
    const isEdit = ref(false);
    const rowData = ref({});
    const handleEdit = (row: Role) => {
        rowData.value = { ...row };
        isEdit.value = true;
        visible.value = true;
    };
    const updateData = () => {
        closeDialog();
        getData();
    };
    const closeDialog = () => {
        visible.value = false;
        isEdit.value = false;
        rowData.value = {};
    };

    // 查看详情弹窗相关
    const visible1 = ref(false);
    const viewData = ref({
        row: {},
        list: [],
        column: 1
    });
    const handleView = async (row: Role) => {
        let res = await agentStarGoodsDetail({
            goods_id: row.id
        })
        if (res.code == 1) {
            viewData.value.row = { ...res.data }
            viewData.value.list = [
                {
                    prop: 'id',
                    label: '商品ID',
                },
                {
                    prop: 'type',
                    label: '商品分类',
                },
                {
                    prop: 'title',
                    label: '商品名称',
                },
                {
                    prop: 'status',
                    label: '状态',
                },
                {
                    prop: 'month',
                    label: '月数/笔数',
                },
                {
                    prop: 'cost_price',
                    label: '成本价',
                },
                {
                    prop: 'sale_price',
                    label: '销售价',
                },
                {
                    prop: 'profit',
                    label: '利润',
                },
            ]
            visible1.value = true;
        }
    };

    // 删除相关
    const handleDelete = (row: Role) => {
        ElMessage.success('删除成功');
    }


    // 权限管理弹窗相关
    const visible2 = ref(false);
    const permissOptions = ref({})
    const handlePermission = (row: Role) => {
        visible2.value = true;
        permissOptions.value = {
            id: row.id,
            permiss: row.permiss
        };
    }

    // 修改价格弹出
    const changeSalePrice = (data) => {
        visiblePrice.value = true
        sale.value.price = data.sale_price
        sale.value.goods_id = data.id
    }
    const submitPrice = async () => {
        visiblePrice.value = false
        let res = await agentModifyStarGoodsPrice(sale.value)
        if (res.code == 1) {

            getData();
        }
        ElMessage.success(res.msg);
    }
    const closePriceDialog = () => {
        visiblePrice.value = false;
        sale.value.price = ''
        sale.value.goods_id = ''
    }
</script>

<style scoped>
    .salePrice {
        margin: 12px;

    }
</style>